'''Simple data loader module.
Loads data files from the "data" directory shipped with a game.
Enhancing this to handle caching etc. is left as an exercise for the reader.
'''

import os
import pygame
import constants

data_py = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.normpath(os.path.join(data_py, '..', constants.DATA_DIR))

def filepath(filename):
    '''Determine the path to a file in the data directory.'''
    return os.path.join(data_dir, filename)

def load(filename, mode='rb'):
    '''Open a file in the data directory.
    "mode" is passed as the second arg to open().
    '''
    return open(os.path.join(data_dir, filename), mode)

def load_image(file, transparent=False):
    if pygame.display.get_init():
        file = os.path.join(data_dir, file)
        image = pygame.image.load(file)
        colorkey = image.get_at((0, 0))
        if transparent is True:
            image.set_colorkey(colorkey, pygame.RLEACCEL)
        return image.convert_alpha()
    else: #running tests?
        return pygame.Surface((10,10))

